package org.jeecg.modules.xm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhaodui.fxy.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.uwms.Uwmsdata;
import org.jeecg.modules.xm.entity.*;
import org.jeecg.modules.xm.service.*;
import org.jeecg.modules.xm.vo.XmMain01Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

import static org.assertj.core.util.DateUtil.now;


/**
 * @Description: xm_main_01
 * @Author: zhaodui
 * @Date: 2020-03-14
 * @Version: V1.0
 */
@RestController
@RequestMapping("/xm01/xmMain01")
@Slf4j
@Api(tags="贴息")
public class XmMain0101Controller extends JeecgController<XmMain01, IXmMain01Service> {
    @Autowired
    private IXmBase01Service xmBase01Service;

    @Autowired
    private IXmMain01Service xmMain01Service;


    @Autowired
    private IXmBase03Service xmBase03Service;

    @Autowired
    private IXmBase02Service xmBase02Service;

    @Value("${jeecg.path.upload}")
    private String upLoadPath;



    @GetMapping(value = "/listyrl")
    @ApiOperation("登录接口")
     public Result<?> queryPageListyrl(XmMain01 xmMain01,
                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                      HttpServletRequest req) {
        xmMain01.setQuery18("已认领");
        String rluser = "";
        try{
            LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
            System.out.println("sysUser+"+sysUser.getUsername()+sysUser.getRealname());

            rluser = sysUser.getUsername();
        }catch (Exception e){
        }
        if(StringUtil.isNotEmpty(rluser)){
            xmMain01.setQuery21(rluser);
        }
        QueryWrapper<XmMain01> queryWrapper = QueryGenerator.initQueryWrapper(xmMain01, req.getParameterMap());
        Page<XmMain01> page = new Page<XmMain01>(pageNo, pageSize);
        IPage<XmMain01> pageList = xmMain01Service.page(page, queryWrapper);

        List<XmMain01> listnew = new ArrayList<>();
        for(XmMain01 t:pageList.getRecords()){
            try{
                if(StringUtil.isEmpty(t.getQuery06())) {
                    QueryWrapper<XmBase01> queryWrapper01 = new QueryWrapper<XmBase01>();
                    queryWrapper01.eq("query03", t.getQuery03());
                    List<XmBase01> list01 = xmBase01Service.list(queryWrapper01);
                    t.setQuery06(list01.get(0).getQuery06());
                    t.setQuery07(list01.get(0).getQuery07());
                }
            }catch (Exception e){

            }
            try{

                QueryWrapper<XmBase02> queryWrapper02 = new QueryWrapper<XmBase02>();
                queryWrapper02.eq("query03", t.getQuery09());
                List<XmBase02> list02 = xmBase02Service.list(queryWrapper02);

                t.setQuery22(list02.get(0).getQuery22());
                t.setQuery23(list02.get(0).getQuery23());
                t.setQuery24(list02.get(0).getQuery24());
                t.setQuery25(list02.get(0).getQuery25());
                t.setQuery19(getmsg(t.getId()));
                t.setQuery26(list02.get(0).getQuery18());
                t.setQuery27(list02.get(0).getQuery19());
                t.setQuery28(list02.get(0).getQuery20());
                t.setQuery29(list02.get(0).getQuery21());
                t.setQuery30(list02.get(0).getQuery26());

            }catch (Exception e){

            }
            listnew.add(t);
        }
        pageList.setRecords(listnew);
        return Result.ok(pageList);
    }















    public  String getmsg(String mainid){
        String message ="在白名单";
        XmMain01 xmMain01 = xmMain01Service.getById(mainid);
        String name = xmMain01.getQuery08();
        String uid = xmMain01.getQuery09();
        try {
            QueryWrapper<XmBase03> queryWrapper03 = new QueryWrapper<XmBase03>();
            queryWrapper03.eq("query14", name);
            queryWrapper03.eq("query13", uid);
            List<XmBase03> list03 = xmBase03Service.list(queryWrapper03);
            if (list03 == null || list03.size() == 0) {
                message = "企业不在白名单";
            }
        } catch (Exception e) {
        }
//行业
        //人数
        //主营业务收入
        //营业收入
        String hangye = "";
        int renshu = 1;
        Double zhuyingyewu = 1.00;
        Double yingyeshouru = 1.00;
        try {
            hangye = xmMain01.getQuery12();
            renshu = Integer.parseInt(xmMain01.getQuery14());
            zhuyingyewu = Double.parseDouble(xmMain01.getQuery15());
            yingyeshouru = Double.parseDouble(xmMain01.getQuery13());
        } catch (Exception e) {

        }


        if (hangye.equals("交通运输业")) {
            if (renshu > 300 && yingyeshouru > 3000) {
                message = message + "很抱歉！签约人数不得多于300人，且营业总收入不得多于3000";
            }
        } else if (hangye.equals("住宿业")) {
            if (renshu > 100 && yingyeshouru > 2000) {
                message = message + "很抱歉！签约人数不得多于100人，且营业总收入不得多于2000";
            }
        } else if (hangye.equals("餐饮业")) {
            if (renshu > 100 && yingyeshouru > 2000) {
                message = message + "很抱歉！签约人数不得多于100人，且营业总收入不得多于2000";
            }
        } else if (hangye.equals("批发业")) {
            if (renshu > 20 && yingyeshouru > 5000) {
                message = message + "很抱歉！签约人数不得多于20人，且营业总收入不得多于5000";
            }
        } else if (hangye.equals("零售业")) {
            if (renshu > 50 && yingyeshouru > 500) {
                message = message + "很抱歉！签约人数不得多于50人，且营业总收入不得多于500";
            }
        } else {
            if (renshu > 100) {
                message = message + "很抱歉！签约人数不得多于100人";
            }
        }

        if ((zhuyingyewu / yingyeshouru) <= 0.7) {
            message = message + "主营业务收入额低于营业总收入70%";

        }
        return  message;
    }

    /**
     *  编辑
     *
     * @param xmMain01Page
     * @return
     */
    @PutMapping(value = "/edit")
    @AutoLog
    public Result<?> editpage(@RequestBody XmMain01Page xmMain01Page) {
        XmMain01 xmMain01 = new XmMain01();
        BeanUtils.copyProperties(xmMain01Page, xmMain01);
        XmMain01 xmMain01Entity = xmMain01Service.getById(xmMain01.getId());
        if("已初审".equals(xmMain01.getQuery18())){
            for(XmItem02 t: xmMain01Page.getXmItem02List()){

                Double tx =  0.00;
                try{
                    tx = Double.parseDouble(t.getQuery22()) ;
                    if(tx==0.00){
                        return Result.error("手工输入贴息不能为0");
                    }
                }catch (Exception e){
                    return Result.error("手工输入贴息不合法");

                }

            }
        }



        if(xmMain01Entity==null) {
            return Result.error("未找到对应数据");
        }
        if("已认领".equals(xmMain01.getQuery18())){
            try{
                LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();

                System.out.println("sysUser+"+sysUser.getUsername()+sysUser.getRealname());
                xmMain01.setQuery21(sysUser.getUsername());
            }catch (Exception e){

            }
        }

        xmMain01Service.updateMain(xmMain01, xmMain01Page.getXmItem02List(),xmMain01Page.getXmItem01List());
        return Result.ok("编辑成功!");
    }









}
